/*
 * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xs;

import io.helidon.config.Config;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.Service;

import javax.json.Json;
import javax.json.JsonObject;

public class GreetService implements Service {
	/**
	 * 读取配置文件中的 “app”字段 部分配置
	 */
	private static final Config CONFIG = Config.create().get("app");

	/**
	 * 读取配置文件中 “app”下层属性“greeting”字段属性值
	 */
	private static String greeting = CONFIG.get("greeting").asString("Hello xs");

	/**
	 * 定义当前Service 路由规则
	 */
	@Override
	public final void update(final Routing.Rules rules) {
		rules
				.get("/", this::getDefaultMessage)
				.get("/{name}", this::getMessage)
				.put("/greeting/{greeting}", this::updateGreeting);
	}

	private void getDefaultMessage(final ServerRequest request, final ServerResponse response) {
		String msg = String.format("%s %s!", greeting, "World");
		JsonObject returnObject = Json.createObjectBuilder()
				.add("message", msg)
				.build();
		response.send(returnObject);
	}

	private void getMessage(final ServerRequest request, final ServerResponse response) {
		//获取问号后面的查询参数
		String age = request.queryParams().first("age").orElse("age 未提供！！！");
		//获取路径参数
		String name = request.path().param("name");
		String msg = name + ": " + age;
		JsonObject returnObject = Json.createObjectBuilder()
				.add("message", msg)
				.build();
		response.send(returnObject);
	}

	/**
	 * 测试put 方法
	 *
	 * @param request
	 * @param response
	 */
	private void updateGreeting(final ServerRequest request, final ServerResponse response) {
		greeting = request.path().param("greeting");
		JsonObject returnObject = Json.createObjectBuilder()
				.add("greeting", greeting)
				.build();
		response.send(returnObject);
	}
}
